<?php
namespace app\index\controller;

use app\index\model\CertModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseListModel;
use app\index\model\CertUseModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginBeianModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginDutyModel;
use app\index\model\EnginJunjieListModel;
use app\index\model\EnginJunjieModel;
use app\index\model\EnginJunyanModel;
use app\index\model\EnginModel;
use app\index\model\EnginTingModel;
use app\index\model\EnginXianModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\IncomeEnginModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouModel;
use app\index\model\UserModel;
use app\index\model\VisaModel;
use app\index\relevan\CertRelevan;
use app\index\relevan\EnginRelevan;
use app\index\service\BudgetService;
use app\index\service\CertService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\File;
use think\Request;

class Engin extends Common
{


    public function list_engin_dao(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number','like', '%'.$param['keyword'].'%'];
        }

        $whereMiss = [];
        if($this->userinfo['token_roletype'] == 1){
            //全部权限
        }
        if($this->userinfo['token_roletype'] == 2){
            $enginArr = (new EnginService())->get_user_engin();
            $whereMiss[] = ['e.id','in',$enginArr];
        }
        if($this->userinfo['token_roletype'] == 3){
            $lianerarr = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where('id','in',$this->userinfo['lianer'])->column('name');
            $enginArr = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1,'business'=>2])
                ->where('lian_unit','in',$lianerarr)->column('id');
            $whereMiss[] = ['e.id','in',$enginArr];
        }
        if($this->userinfo['token_roletype'] == 4){
            $where[] = ['e.id','<',0];
        }
        if($this->userinfo['token_roletype'] == 5){
            $userChanid = (new UserService())->get_user_chanid();
            $whereMiss[] = ['e.chanid','=',$userChanid];
        }

        $data = EnginModel::alias('e')
            ->field('e.id,e.name as title')
            ->where($where)
            ->where($whereMiss)
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','in',[0,1]];

        $param_business = isset($param['business']) ? $param['business'] : 0;
        if($param_business > 0){
            $where[] = ['e.business','=',$param_business];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|e.jian_unit|e.areas|xiang.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['man_xiang_name']) && !empty($param['man_xiang_name'])){
            $where[] = ['xiang.realname','like', '%'.$param['man_xiang_name'].'%'];
        }


        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['e.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['e.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['actualdt']) && !empty($param['actualdt'])){
            $where[] = ['e.actualdt','between', explode(' - ', $param['actualdt'])];
        }
        if(isset($param['filishdt']) && !empty($param['filishdt'])){
            $where[] = ['e.filishdt','between', explode(' - ', $param['filishdt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }



        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';


        $missName = $param_business == 1 ? 'engin' : 'enginlian';



        $data['data'] = EnginModel::alias('e')
            ->field('e.*,xiang.realname as man_xiang_name')
            ->leftJoin('user xiang','xiang.id=e.man_xiang')
            ->where($where)
            ->where((new MenuService())->get_miss_where($missName, 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['state_name_mob'] = (new EnginService())->get_engin_state_name($item['id'], $item['state']);
                $item['isting'] = $item['state'] == 3 ? true : false;

                $item['engintype'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth($missName);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_enginmy(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        $param_business = isset($param['business']) ? $param['business'] : 0;
        if($param_business > 0){
            $where[] = ['e.business','=',$param_business];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|e.jian_unit|e.areas','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }


        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['e.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['e.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['actualdt']) && !empty($param['actualdt'])){
            $where[] = ['e.actualdt','between', explode(' - ', $param['actualdt'])];
        }
        if(isset($param['filishdt']) && !empty($param['filishdt'])){
            $where[] = ['e.filishdt','between', explode(' - ', $param['filishdt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }



        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';



        $fisStr = "find_in_set(".$this->userinfo['id'].",e.man_zhuan)";
        $fisStr .= " or find_in_set(".$this->userinfo['id'].",e.man_zi)";
        $fisStr .= " or find_in_set(".$this->userinfo['id'].",e.man_cheng)";
        $fisStr .= " or find_in_set(".$this->userinfo['id'].",e.man_zhian)";
        $fisStr .= " or e.man_xiang=".$this->userinfo['id'];
        $whereFis = Db::raw($fisStr);


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where($whereFis)
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['state_name_mob'] = (new EnginService())->get_engin_state_name($item['id'], $item['state']);
                $item['isting'] = $item['state'] == 3 ? true : false;

                $item['engintype'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入项目名称');
            }
            if((new EnginService())->check_engin_name($param['name'])){
                return outputJson(4000, '项目名称已存在');
            }
            if($param['business'] == 2){
                if(empty($param['lian_unit'])){
                    return outputJson(4000, '请选择联营方');
                }
            }
            if(empty($param['chanid'])){
                return outputJson(4000, '请选择所属单位');
            }
            if($param['state'] == ''){
                return outputJson(4000, '请选择项目状态');
            }
            if($param['state'] == 0){
                $param['actualdt'] = '';
                $param['filishdt'] = '';
            }
            if($param['state'] == 3){
                $param['filishdt'] = '';
            }

            if($param['business'] == 2){
                if(empty($param['lian_unit'])){
                    return outputJson(4000, '请选择联营方');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['isturn'] = 1;
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = EnginModel::insertGetId($param);
                (new EnginRelevan())->vate_engin_add_mingxi($id);
                (new OptService())->relation_file($files,'engin',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin', $id, '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
            $data['areas'] = (new OptService())->get_city_cader();
            $data['state'] = (new EnginService())->get_engin_state();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }

    public function edit_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入项目名称');
            }
            if((new EnginService())->check_engin_name($param['name'], $param['id'])){
                return outputJson(4000, '项目名称已存在');
            }
            if($param['business'] == 2){
                if(empty($param['lian_unit'])){
                    return outputJson(4000, '请选择联营方');
                }
            }
            if(empty($param['chanid'])){
                return outputJson(4000, '请选择所属单位');
            }
            if($param['state'] == ''){
                return outputJson(4000, '请选择项目状态');
            }
            if($param['state'] == 0){
                $param['actualdt'] = '';
                $param['filishdt'] = '';
            }
            if($param['state'] == 3){
                $param['filishdt'] = '';
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $oldinfo = EnginModel::where(['id'=>$param['id']])->find();

            Db::startTrans();
            try {
                EnginModel::update($param);

                if($oldinfo['state'] == 0 || $oldinfo['state'] == 3){
                    if($param['state'] == 6){
                        (new CertRelevan())->set_cert_statueuse_engin($param['id']);
                    }
                }else{
                    if($param['state'] == 0 || $param['state'] == 3){
                        (new CertRelevan())->set_cert_statueuse_engin($param['id']);
                    }
                }

                (new OptService())->relation_file($files,'engin',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['man_xiang_info'] = (new UserService())->get_users_part_arr($data['data']['man_xiang'], 'user');
            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
            $data['areas'] = (new OptService())->get_city_cader();
            $data['state'] = (new EnginService())->get_engin_state();

            $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function del_engin(Request $request){
        $param = $request->post();
        $data = EnginModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin', $data['id'], '项目');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }

    public function show_engin(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['man_xiang_name'] = UserModel::get_realname_by_pk($data['data']['man_xiang']);
        $data['data']['engintype'] = (new OptService())->get_base_showname('engin_engintype', $data['data']['engintype']);
        $data['data']['zhaotype'] = (new OptService())->get_base_showname('bid_bidding_type', $data['data']['zhaotype']);
        $data['data']['state_name'] = (new EnginService())->get_engin_state_name($data['data']['id'], $data['data']['state']);
        $data['data']['business_name'] = $data['data']['business'] == 1 ? '自营' : '联营';
        $data['data']['chanid_name'] = DeptModel::where(['id'=>$data['data']['chanid']])->value('name');



        $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['id'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->find();
        $data['shiinfo'] = $shiinfo;

        $data['data']['shi_money'] = $shiinfo['money'];
        $data['data']['shi_money'] += ContractBuModel::where(['shiid'=>$shiinfo['id'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->sum('money');
        $data['data']['shiinfoid'] = isset($shiinfo['id']) ? $shiinfo['id'] : 0;


        $budget = EnginBudgetModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['id'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])->find();
        $data['data']['budgetid'] = isset($budget['id']) ? $budget['id'] : 0;

        $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function kaijun_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['state'] == ''){
                return outputJson(4000, '请选择变更状态');
            }
            if($param['state'] == 0){
                $param['actualdt'] = '';
                $param['filishdt'] = '';
            }
            if($param['state'] == 3){
                $param['filishdt'] = '';
            }

            unset($param['token']);

            $statusname = '';
            if($param['state'] == 0){
                $statusname = '未开工';
            }
            if($param['state'] == 3){
                $statusname = '在建';
            }
            if($param['state'] == 6){
                $statusname = '竣工';
            }

            $oldinfo = EnginModel::where(['id'=>$param['id']])->find();

            Db::startTrans();
            try {
                EnginModel::update($param);

                if($oldinfo['state'] == 0 || $oldinfo['state'] == 3){
                    if($param['state'] == 6){
                        (new CertRelevan())->set_cert_statueuse_engin($param['id']);
                    }
                }else{
                    if($param['state'] == 0 || $param['state'] == 3){
                        (new CertRelevan())->set_cert_statueuse_engin($param['id']);
                    }
                }

                (new OptService())->add_flow_log('开竣工', 'engin', $param['id'],$statusname);
                (new OptService())->add_log_list('开竣工', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['state_name'] = (new EnginService())->get_engin_state_name($data['data']['id'], $data['data']['state']);
            $data['state'] = (new EnginService())->get_engin_state();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function ting_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $engin = EnginModel::where(['id'=>$param['id']])->find();
            if(!$engin){
                return outputJson(4000,lang('code_4000'));
            }
            if($engin['state'] != 3){
                return outputJson(4000, lang('code_4000'));
            }

            if(empty($param['dt'])){
                return outputJson(4000, '请选择日期');
            }


            $param['enginid'] = $engin['id'];
            $param['created_user'] = $this->userinfo['id'];
            $param['created_at'] = date('Y-m-d H:i:s');


            unset($param['id']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = EnginTingModel::insertGetId($param);
                (new OptService())->add_flow_log('停复工', 'engin', $param['enginid'], $param['name'], $param['explain']);
                (new OptService())->add_log_list('停复工', 'engin', $param['enginid'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            if($data['data']['state'] != 3){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['tingname'] = '停工';
            $tingNew = EnginTingModel::where(['enginid'=>$data['data']['id']])->order('id DESC')->find();
            if($tingNew){
                if($tingNew['name'] == '停工'){
                    $data['data']['tingname'] = '复工';
                }
            }
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function beian_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        CertUseListModel::update($item);
                    }
                }

                (new OptService())->add_flow_log('编辑备案人员', 'engin', $param['id']);
                (new OptService())->add_log_list('编辑备案人员', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = CertUseListModel::alias('list')->field('list.*')
                ->leftJoin('cert_use use','use.id = list.mid')
                // ->where(['use.enginid'=>$id,'use.deleted_user'=>0,'use.type'=>2,'list.pid'=>0,'list.isdel'=>0])
                ->where(['use.enginid'=>$id,'use.deleted_user'=>0,'use.type'=>2])
                ->select()->toArray();
            foreach ($list as &$v){
                $cert_data = CertModel::where('id',$v['cert_id'])->find();
                $v['cert_name'] = $cert_data['name'];
                $v['cert_number'] = $cert_data['number'];
                $v['cert_major'] = str_replace('[]', ',', $cert_data['major']);
                $v['person_name'] = CertPersonModel::where('id',$cert_data['personid'])->value('name');
                $v['use'] = '';
                $v['com_name'] = DeptModel::get_name_by_pk($cert_data['chanid']);
                $v['issuo_name'] = $v['issuo'] == 1 ? '是' : '否';
            }
            $data['list'] = $list;
            $data['issuo'] = CertUseModel::get_issuo();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function beian_engin1(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                EnginBeianModel::where(['enginid' => $param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['enginid'] = $param['id'];
                        EnginBeianModel::insert($item);
                    }
                }
                (new OptService())->add_flow_log('编辑备案人员', 'engin', $param['id']);
                (new OptService())->add_log_list('编辑备案人员', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = EnginBeianModel::where(['enginid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['cert_name'] = '';
                $item['cert_number'] = '';
                $item['cert_major'] = '';
                $item['person_name'] = '';
                $cert = CertModel::where(['id'=>$item['certid']])->find();
                if($cert){
                    $person = CertPersonModel::where(['id'=>$cert['personid']])->find();
                    $item['cert_name'] = $cert['name'];
                    $item['cert_number'] = $cert['number'];
                    $item['cert_major'] = str_replace('[]',',',$cert['major']);
                    $item['person_name'] = $person['name'];
                }
            }

            $data['list'] = $list;

            $data['list'] = $list;
            $data['beiangang'] = (new OptService())->get_base_info('engin_beiangang');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function xian_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                EnginXianModel::where(['enginid' => $param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['enginid'] = $param['id'];
                        EnginXianModel::insert($item);
                    }
                }
                (new OptService())->add_flow_log('编辑现场人员', 'engin', $param['id']);
                (new OptService())->add_log_list('编辑现场人员', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = EnginXianModel::where(['enginid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as $k=>&$v){
                    $v['beiangang_name'] = (new OptService())->get_base_showname('engin_beiangang', $v['beiangang']);
            }
            $data['list'] = $list;
            $data['beiangang'] = (new OptService())->get_base_info('engin_beiangang');
            $data['beiangangOrder'] = (new OptService())->get_base_order('engin_beiangang');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function manage_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            unset($param['token']);

            Db::startTrans();
            try {
                EnginModel::update($param);
                (new OptService())->add_flow_log('编辑项目管理组', 'engin', $param['id']);
                (new OptService())->add_log_list('编辑项目管理组', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['business_name'] = $data['data']['business'] == 1 ? '自营' : '联营';
            $data['data']['man_xiang_info'] = (new UserService())->get_users_part_arr($data['data']['man_xiang'], 'user');
            $data['data']['man_zhuan_info'] = (new UserService())->get_users_part_arr($data['data']['man_zhuan'], 'user');
            $data['data']['man_zi_info'] = (new UserService())->get_users_part_arr($data['data']['man_zi'], 'user');
            $data['data']['man_cheng_info'] = (new UserService())->get_users_part_arr($data['data']['man_cheng'], 'user');
            $data['data']['man_zhian_info'] = (new UserService())->get_users_part_arr($data['data']['man_zhian'], 'user');

            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function duty_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                EnginDutyModel::where(['enginid' => $param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['enginid'] = $param['id'];
                        EnginDutyModel::insert($item);
                    }
                }
                (new OptService())->add_flow_log('编辑责任主体', 'engin', $param['id']);
                (new OptService())->add_log_list('编辑责任主体', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = EnginDutyModel::where(['enginid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function files_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $files = array_filter($param['files']);
            unset($param['files']);

            Db::startTrans();
            try {
                (new OptService())->relation_file($files,'engin',$param['id']);
                (new OptService())->add_flow_log('编辑项目附件', 'engin', $param['id']);
                (new OptService())->add_log_list('编辑项目附件', 'engin', $param['id'], '项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }




    public function list_engin_junyan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['yan.deleted_user','=', 0];
        $where[] = ['yan.comid','=',$this->userinfo['comid']];
        $where[] = ['yan.isturn','=',1];
        $where[] = ['yan.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|yan.she_unit|yan.li_unit|yan.yanjie|yan.expwen|yan.expjian','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['yan.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['buture']) && !empty($param['buture'])){
            $where[] = ['yan.buture','=', $param['buture']];
        }
        if(isset($param['she_unit']) && !empty($param['she_unit'])){
            $where[] = ['yan.she_unit','like', '%'.$param['she_unit'].'%'];
        }
        if(isset($param['li_unit']) && !empty($param['li_unit'])){
            $where[] = ['yan.li_unit','like', '%'.$param['li_unit'].'%'];
        }
        if(isset($param['actualdt']) && !empty($param['actualdt'])){
            $where[] = ['yan.actualdt','between', explode(' - ', $param['actualdt'])];
        }
        if(isset($param['filishdt']) && !empty($param['filishdt'])){
            $where[] = ['yan.filishdt','between', explode(' - ', $param['filishdt'])];
        }
        if(isset($param['yanjie']) && !empty($param['yanjie'])){
            $where[] = ['yan.yanjie','=', $param['yanjie']];
        }
        if(isset($param['yandt']) && !empty($param['yandt'])){
            $where[] = ['yan.yandt','between', explode(' - ', $param['yandt'])];
        }
        if(isset($param['expwen']) && !empty($param['expwen'])){
            $where[] = ['yan.expwen','like', '%'.$param['expwen'].'%'];
        }
        if(isset($param['expjian']) && !empty($param['expjian'])){
            $where[] = ['yan.expjian','like', '%'.$param['expjian'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['yan.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['buture'] = (new EnginService())->get_engin_buture();
        $data['is_buture'] = isset($param['buture']) ? $param['buture'] : '';

        $data['yanjie'] = (new EnginService())->get_engin_yanjie();
        $data['is_yanjie'] = isset($param['yanjie']) ? $param['yanjie'] : '';


        $data['data'] = EnginJunyanModel::alias('yan')
            ->field('yan.*,e.name as enginname,e.number as enginnumber,e.business,e.chanid as enginchanid')
            ->join('engin e','e.id=yan.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_junyan', 'yan'))
            ->order('yan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['enginchanid_name'] = DeptModel::get_name_by_pk($item['enginchanid']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';

                $item['shiid'] = 0;
                $item['shi_name'] = '';
                $item['shi_faer'] = '';
                $item['shi_chenger'] = '';
                $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
                if($shiinfo){
                    $item['shiid'] = $shiinfo['id'];
                    $item['shi_name'] = $shiinfo['name'];
                    $item['shi_faer'] = $shiinfo['faer'];
                    $item['shi_chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
                }



                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_junyan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_engin_junyan(Request $request){
        $param = $request->post();
        $data = EnginJunyanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginJunyanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_junyan', $data['id'], '竣工验收单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_junyan(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginJunyanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
        if($shiinfo){
            $shiinfo['chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
        }
        $data['shiinfo'] = $shiinfo;


        $data['files'] = (new OptService())->get_file('engin_junyan', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_engin_junjie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|jie.songfee|jie.shen_unit|jie.money|jie.shencha|jie.jiangfee|jie.fafee|jie.zhifee|jie.qifee|jie.yufee','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['shen_unit']) && !empty($param['shen_unit'])){
            $where[] = ['jie.shen_unit','like', '%'.$param['shen_unit'].'%'];
        }
        if(isset($param['songdt']) && !empty($param['songdt'])){
            $where[] = ['jie.songdt','between', explode(' - ', $param['songdt'])];
        }
        if(isset($param['songfee']) && !empty($param['songfee'])){
            $where[] = ['jie.songfee','like', '%'.$param['songfee'].'%'];
        }
        if(isset($param['shendt']) && !empty($param['shendt'])){
            $where[] = ['jie.shendt','between', explode(' - ', $param['shendt'])];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['shencha']) && !empty($param['shencha'])){
            $where[] = ['jie.shencha','like', '%'.$param['shencha'].'%'];
        }
        if(isset($param['jiangfee']) && !empty($param['jiangfee'])){
            $where[] = ['jie.jiangfee','like', '%'.$param['jiangfee'].'%'];
        }
        if(isset($param['fafee']) && !empty($param['fafee'])){
            $where[] = ['jie.fafee','like', '%'.$param['fafee'].'%'];
        }
        if(isset($param['zhidt']) && !empty($param['zhidt'])){
            $where[] = ['jie.zhidt','between', explode(' - ', $param['zhidt'])];
        }
        if(isset($param['zhifee']) && !empty($param['zhifee'])){
            $where[] = ['jie.zhifee','like', '%'.$param['zhifee'].'%'];
        }
        if(isset($param['qifee']) && !empty($param['qifee'])){
            $where[] = ['jie.qifee','like', '%'.$param['qifee'].'%'];
        }
        if(isset($param['yufee']) && !empty($param['yufee'])){
            $where[] = ['jie.yufee','like', '%'.$param['yufee'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $gckshou = (new BudgetService())->get_budshou_sons($gckid);

        $data['data'] = EnginJunjieModel::alias('jie')
            ->field('jie.*,e.name as enginname,e.number as enginnumber,e.business,e.chanid as enginchanid')
            ->join('engin e','e.id=jie.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_junjie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($gckshou){
                $item['enginchanid_name'] = DeptModel::get_name_by_pk($item['enginchanid']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';

                $item['shiid'] = 0;
                $item['shi_name'] = '';
                $item['shi_faer'] = '';
                $item['shi_chenger'] = '';
                $item['shi_money'] = (new EnginService())->get_contract_shi_final_money($item['enginid']);
                $item['qian_money'] = VisaModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('shemoney');
                $item['chan_money'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('leijin');
                $item['hui_money'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['enginid'],'deleted_user'=>0])
                    ->where('budid','in',$gckshou)->sum('money');

                $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
                if($shiinfo){
                    $item['shiid'] = $shiinfo['id'];
                    $item['shi_name'] = $shiinfo['name'];
                    $item['shi_faer'] = $shiinfo['faer'];
                    $item['shi_chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
                }

                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_junjie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_engin_junjie(Request $request){
        $param = $request->post();
        $data = EnginJunjieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginJunjieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_junjie', $data['id'], '竣工结算单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_junjie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginJunjieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
        if($shiinfo){
            $shiinfo['chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
        }
        $data['shiinfo'] = $shiinfo;
        $data['data']['shi_money'] = (new EnginService())->get_contract_shi_final_money($data['data']['enginid']);
        $data['data']['qian_money'] = VisaModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('shemoney');
        $data['data']['chan_money'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('leijin');

        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $gckshou = (new BudgetService())->get_budshou_sons($gckid);
        $data['data']['hui_money'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'deleted_user'=>0])
            ->where('budid','in',$gckshou)->sum('money');

        $list = EnginJunjieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;


        $data['files'] = (new OptService())->get_file('engin_junjie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_engin_elecfile(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereStatus[] = ['isturn','=',1];
        $whereStatus[] = ['status','=',1];


        if(!isset($param['enginid']) || empty($param['enginid'])){
            return outputJson(2000,lang('code_2000'),['data'=>[]]);
        }


        $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();

        $modArr = [
            'engin' => '项目',
            'contract_shi' => '施工合同',
            'contract_cai' => '采购合同',
            'engin_budget' => '成本预算',
            'engin_budbian' => '预算变更',
            'payment_engin' => '项目付款(供应商)',
            'payment_neifu' => '内包付款',
            'payment_neikou' => '内包扣款',
            'payment_bao' => '项目费用报销',
            'income_engin' => '项目收款',
            'engin_bond_jiao' => '项目保证金',
            'engin_bond_fan' => '项目保证金',
            'engin_bond_shou' => '项目保证金',
            'engin_bond_kai' => '项目保证金',
            'piao_kai' => '项目开票',
            'piao_shou' => '项目收票',
            'piao_tax' => '项目税费预缴',
            'stuff_cai' => '材料采购单',
            'stuff_ru' => '材料入库单',
            'stuff_jie' => '材料结算单',
            'labour_shen' => '劳务进度款申报',
            'labour_jie' => '劳务完工结算',
            'labour_ling' => '劳务零星劳务单',
            'device_jin' => '租赁进场登记',
            'device_jie' => '租赁结算',
            'device_lin' => '临时机械登记',
            'subbao_shen' => '分包进度款申报',
            'subbao_jie' => '分包完工结算',
            'engin_xun' => '巡检报告',
            'engin_zheng' => '整改通知',
            'speed_zong' => '总进度计划',
            'speed_month' => '月进度计划',
            'engin_diary' => '施工日志',
            'engin_speed' => '进度填报',
            'visa' => '签证单',
            'engin_chan' => '产值填报(甲方)',
            'engin_junyan' => '竣工验收单',
            'engin_junjie' => '竣工结算单',
            'engin_stamp' => '资料盖章'
        ];

        $list = [];
        foreach ($modArr as $tabname=>$tabtitle){
            if($tabname == 'engin'){
                $idarr = Db::name($tabname)->where($where)->where($whereStatus)->where(['id'=>$param['enginid']])->column('id');
            }elseif($tabname == 'engin_bond_fan'){
                $shouidarr = EnginBondShouModel::where($where)->where($whereStatus)->where(['id'=>$param['enginid']])->column('id');
                $idarr = Db::name($tabname)->where($where)->where($whereStatus)->where(['shouid'=>$shouidarr])->column('id');
            }else{
                $idarr = Db::name($tabname)->where($where)->where($whereStatus)->where(['enginid'=>$param['enginid']])->column('id');
            }

            $file = FileModel::field('id,table,mid,name,ext,size,encpath,fileplain,created_user,created_at,filenav')
                ->where(['comid'=>$this->userinfo['comid'],'table'=>$tabname])
                ->where('mid','in', $idarr)->select()->toArray();
            foreach ($file as $item){
                $item['title'] = $tabtitle;
                $item['enginname'] = $engininfo['name'];
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['filenav_name'] = (new OptService())->get_base_showname('engin_filenav', $item['filenav']);
                $list[] = $item;
            }
        }

        $subSort = array_column($list,'created_at');
        array_multisort($subSort,SORT_DESC,$list);

        $newData = [];
        $start = ($param['page']-1)*$param['pagesize'];
        for ($i=$start; $i<$start+$param['pagesize']; $i++){
            if(isset($list[$i])){
                $newData[] = $list[$i];
            }
        }

        $data['data']['current_page'] = $param['page'];
        $data['data']['data'] = $newData;
        $data['data']['last_page'] = ceil(count($list)/$param['pagesize']);
        $data['data']['per_page'] = $param['pagesize'];
        $data['data']['total'] = count($list);


        $data['butauth'] = (new MenuService())->get_butauth('engin_elecfile');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function gui_engin_elecfile(Request $request){
        if($request->isPost()){
            $param = $request->post();

            Db::startTrans();
            try {
                FileModel::where('id','in',$param['ids'])->update(['filenav'=>$param['filenav']]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['filenav'] = (new OptService())->get_base_info('engin_filenav');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_elecfile(Request $request){
        $param = $request->post();
        $ids = array_filter(explode(',',$param['id']));
        if(count($ids) == 0){
            return outputJson(4000,'请选择需要删除的数据');
        }
        Db::startTrans();
        try {
            FileModel::where('id','in',$param['id'])->delete();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function rename_engin_elecfile(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入文件名称');
            }

            Db::startTrans();
            try {
                FileModel::where('id','in',$param['id'])->update(['name'=>$param['name']]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id', 0, 'int');
            $data['data'] = FileModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function down_engin_elecfile(Request $request){
        if($request->isPost()){
            $enginid = $request->post('enginid', 0, 'string');
            $engininfo = EnginModel::get_info_by_pk($enginid);
            $ids = $request->post('ids', 0, 'string');
            $fileArr = FileModel::where('id','in',$ids)->select()->toArray();
            if(!$engininfo || !$fileArr){
                return outputJson(4000, '下载失败,无附件信息');
            }

            $file_dir = 'elecfile/'.$this->userinfo['id'].'/';
            if(file_exists($file_dir)){
                $existAtt = scandir($file_dir);
                foreach ($existAtt as $value){
                    if($value != '.' && $value != '..'){
                        unlink($file_dir.$value);
                    }
                }
            }else{
                mkdir($file_dir, 0777);
            }


            $filename = $engininfo['name'].'-电子文件-'.date('YmdHis').'.zip';
            $zip = new \ZipArchive();
            $res = $zip->open($file_dir.$filename, $zip::CREATE);
            if ($res === TRUE) {
                foreach ($fileArr as $_file) {
                    $zip->addFile($_file['oripath'], $_file['name']);
                }
            }
            $zip->close();

            if(file_exists($file_dir.$filename)){
                return outputJson(2000,lang('code_2000'), $file_dir.$filename);
            }else{
                return outputJson(4000, '下载失败');
            }
        }
    }



}
